###
# @Description:
# @Author: Chetwyn
# @Date: 2019-05-20 11:13:48
# @LastEditors: Chetwyn
# @LastEditTime: 2019-11-11 17:01:28
###
#!/bin/bash
set -e
set -x

if [ $# -lt 2 ]; then
    echo "Usage: $0 [d|r|p] [commitId|branch|tag]"
    exit
fi

echo '>>>clear cache'
rm -rf bundle/

echo '>>>create bundle folder'
mkdir -p bundle/android && mkdir -p bundle/ios

echo '>>>start build...'
yarn install && yarn run build_android && yarn run build_ios

echo '>>>build finished'

echo '>>>start zip'

app_dir="$(cd "$(dirname "$0")" && pwd)"
ios_dir=$app_dir/bundle/ios
android_dir=$app_dir/bundle/android

current_date=$(date +%Y%m%d-%H%M%S)

target=$current_date

case $1 in
d | develop | r | release)
    target=$2-${current_date}
    ;;

p | production)
    target=v$2-${current_date}
    ;;
*)
    echo "参数输入错误, 请使用: $0 [d|r|p] [commitId|branch|tag]"
    exit
    ;;
esac

echo '>>>start zip ios'
cd $ios_dir && zip -r ../我的客户-ios-${target}.zip *

echo '>>>start zip android'
cd $android_dir && zip -r ../我的客户-android-${target}.zip *

echo '>>>push zip files'
cd ${app_dir}/bundle

# 这里是在网盘创建对应小程序的文件夹，如文件夹已存在则会创建失败(不影响)，C端用 “Client-C”，B端用“Client-B”，后面是仓库名称。
# ！！！注意，文件夹避免使用中文名称，如要使用中文，请先使用urlencode转码后使用。
curl -u cgws:cgws@Zaq1 -X MKCOL "http://nextcloud.fintech.com/remote.php/webdav/RN-Meppo-Packages/Client-B/Meppo-CustomerManagement/"

# 上传Android包
curl -u cgws:cgws@Zaq1 -T 我的客户-android-${target}.zip http://nextcloud.fintech.com/remote.php/webdav/RN-Meppo-Packages/Client-B/Meppo-CustomerManagement/

# 上传iOS包
curl -u cgws:cgws@Zaq1 -T 我的客户-ios-${target}.zip http://nextcloud.fintech.com/remote.php/webdav/RN-Meppo-Packages/Client-B/Meppo-CustomerManagement/

# 创建Android包分享下载链接
curl -u cgws:cgws@Zaq1 --header "OCS-APIRequest: true" -X POST -d "path=/RN-Meppo-Packages/Client-B/Meppo-CustomerManagement/我的客户-android-${target}.zip&shareType=3&permissions=1" "http://nextcloud.fintech.com/ocs/v2.php/apps/files_sharing/api/v1/shares"

# 创建iOS包分享下载链接
curl -u cgws:cgws@Zaq1 --header "OCS-APIRequest: true" -X POST -d "path=/RN-Meppo-Packages/Client-B/Meppo-CustomerManagement/我的客户-ios-${target}.zip&shareType=3&permissions=1" "http://nextcloud.fintech.com/ocs/v2.php/apps/files_sharing/api/v1/shares"
